local shangshu = fk.CreateSkill {
  name = "lingling__shangshu",
}

Fk:loadTranslationTable {
  ["lingling__shangshu"] = "上书",
  [":lingling__shangshu"] = "出牌阶段限一次，你可以观看一名其他角色的所有手牌，然后你可以弃置你和其共计四张不同花色的手牌，若你弃置的牌更多，你摸等量牌。",

  ["#lingling__shangshu"] = "上书：选择一名有手牌的其他角色，并可弃置你与其手牌中共计四张花色各不相同的牌",
  ["#lingling__shangshu-ask"] = "上书：弃置双方手里四张不同花色的牌",
  ["lingling__shangshu_discard"] = "上书",
}

Fk:addPoxiMethod {
  name = "lingling__shangshu_discard",
  prompt = "#lingling__shangshu-ask",
  card_filter = function(to_select, selected, data)
    local suit = Fk:getCardById(to_select).suit
    if suit == Card.NoSuit then return false end
    return not table.find(selected, function(id) return Fk:getCardById(id).suit == suit end)
        and not (Self:prohibitDiscard(Fk:getCardById(to_select)) and table.contains(data[1][2], to_select))
  end,
  feasible = function(selected)
    return #selected == 4
  end,
}

shangshu:addEffect("active", {
  anim_type = "control",
  prompt = "#lingling__shangshu",
  card_num = 0,
  target_num = 1,
  can_use = function(self, player)
    return player:usedSkillTimes(shangshu.name, Player.HistoryPhase) < 1
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, player, to_select, selected)
    return #selected == 0 and to_select ~= player and not to_select:isKongcheng()
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    local player_hands = player:getCardIds("h")
    local target_hands = target:getCardIds("h")
    local cards = room:askToPoxi(player, {
      poxi_type = "lingling__shangshu_discard",
      data = {
        { player.general, player_hands },
        { target.general, target_hands },
      },
      cancelable = true,
    })
    if #cards == 0 then return end
    local cards1 = table.filter(cards, function(id) return table.contains(player_hands, id) end)
    local cards2 = table.filter(cards, function(id) return table.contains(target_hands, id) end)
    local moveInfos = {}
    if #cards1 > 0 then
      table.insert(moveInfos, {
        from = player.id,
        ids = cards1,
        toArea = Card.DiscardPile,
        moveReason = fk.ReasonDiscard,
        proposer = effect.from,
        skillName = shangshu.name,

      })
    end
    if #cards2 > 0 then
      table.insert(moveInfos, {
        from = target.id,
        ids = cards2,
        toArea = Card.DiscardPile,
        moveReason = fk.ReasonDiscard,
        proposer = effect.from,
        skillName = shangshu.name,
      })
    end
    room:moveCards(table.unpack(moveInfos))
    if player.dead then return end
    if #cards1 > 2 then
      room:drawCards(player, #cards1, shangshu.name)
    end
  end,
})

return shangshu
